package com.start_eternity.oasissafewalk.controller;

import com.start_eternity.oasissafewalk.comment.Result;
import com.start_eternity.oasissafewalk.pojo.*;
import com.start_eternity.oasissafewalk.pojo.dto.StationDTO;
import com.start_eternity.oasissafewalk.service.OrderService;
import com.start_eternity.oasissafewalk.service.StationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@Slf4j
@RestController
@RequestMapping("/stations")
public class StationController {

    @Autowired
    private StationService stationService;

    @Autowired
    private OrderService orderService;

    /*//查询站点
    @GetMapping
    public Result list(){
        log.info("查询所有站点数据");

        //调用service查询站点数据
        List<Station> stationList = stationService.list();

        return Result.success(stationList);
    }*/

    //查询站点
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String stationName, Short stationStatus,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("分页查询,参数:{},{},{},{},{},{}", page, pageSize, stationName, stationStatus, begin, end);

        //调用service方法进行分页查询
        PageBean pageBean = stationService.page(page, pageSize, stationName, stationStatus, begin, end);
        return Result.success(pageBean);
    }

    //新增站点
    @PostMapping
    public Result add(@RequestBody Station station) {
        log.info("添加站点");

        stationService.add(station);

        return Result.success();
    }

    //删除站点
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("根据id删除站点:{}", id);

        stationService.delete(id);

        return Result.success();
    }

    //修改站点
    //获取站点id
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("获取站点id：{}", id);

        Station station = stationService.getById(id);

        return Result.success(station);
    }

    //修改
    @PutMapping
    public Result update(@RequestBody Station station) {
        log.info("修改站点:{}", station);

        stationService.update(station);

        return Result.success();
    }

    @GetMapping("/order/{id}")
    public Result reviewOrder(@PathVariable Integer id) {
        log.info("判断是否有站点满足该订单");

        //通过id获取订单信息
        Order order = orderService.queryOrderById(id);

        //判断有无站点满足订单，如果有，就返回最合适的站点，如果无就为空
        StationDTO stationDTO = stationService.getStationDTO(order);

        //判断起始站点和结束站点是否为空，如果都不为空就返回起始站点和结束站点，如果有一个为空就需要管理员审核
        if ((stationDTO.getStartStation() != null) && (stationDTO.getEndStation() != null)) {
            return Result.success(stationDTO);
        }

        if ((stationDTO.getStartStation() == null) && (stationDTO.getEndStation() != null)){
            return Result.examine("站点表中没有符合订单的起始站点，需要审核",order);
        }
        if ((stationDTO.getStartStation() != null) && (stationDTO.getEndStation() == null)){
            return Result.examine("站点表中没有符合订单的结束站点，需要审核",order);
        }

        return Result.examine("站点表中没有符合订单的起始站点和结束站点，需要审核", order);


        /*if () {
            if (stationDTO.getStartStation() == null) {
                Station station = new Station();
                station.setLatitude(order.getStartLatitude());
                station.setLongitude(order.getStartLongitude());
                station.setCreateTime(LocalDateTime.now());
                station.setUpdateTime(LocalDateTime.now());
                stationService.add(station);
            }

            if (stationDTO.getEndStation() == null) {
                Station station = new Station();
                station.setLatitude(order.getEndLatitude());
                station.setLongitude(order.getEndLongitude());
                station.setCreateTime(LocalDateTime.now());
                station.setUpdateTime(LocalDateTime.now());
                stationService.add(station);
            }

            return Result.success("审核通过");
        }

        return Result.success("审核不通过");*/


    }
}
